package com.cdream.basic.algorithm.leetcode.string;

/**
 * @author cdream
 * @date 10/15/21
 */
public class Question38 {
    public String countAndSay(int n) {
        String[] dp =  new String[n+1];
        dp[1] = "1" ;
        for(int i = 2; i<=n ;i++){
            String s = dp[i-1];
            int count = 0;
            char c;
            StringBuilder res = new StringBuilder();
            for(int j = 0; j<s.length(); j++){
                c = s.charAt(j);
                count++;
                if(j==s.length()-1 || c!=s.charAt(j+1)){
                    res.append(count);
                    res.append(c);
                    count = 0;
                }
            }
            dp[i] = res.toString();
        }
        return dp[n];
    }

}
